package com.zdd.chap2.controller;

import com.zdd.chap2.dao.UserDao;
import com.zdd.chap2.entity.User;
import com.zdd.chap2.vo.ResultVo;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/userTest")
public class UserController {
    @GetMapping("/{id}")
    public ResultVo getUser(@PathVariable int id){
        User user= UserDao.findUserById(id);
        return ResultVo.success("查找指定用户成功",true,user);
    }

    @GetMapping("")
    public ResultVo getAllUsers(){
        Map<Integer,User> users=UserDao.findAllUsers();
        return ResultVo.success("查找所有用户信息成功",true,users);
    }

    @PostMapping("")
    public ResultVo addUser(@RequestBody User user){
        UserDao.addUser(user);
        return ResultVo.success("新增用户成功",true,null);
    }

    @PutMapping("")
    public ResultVo updateUser(@RequestBody User user){
        UserDao.updateUser(user);
        return ResultVo.success("修改用户成功",true,null);
    }

    @DeleteMapping("/{id}")
    public ResultVo deleteUser(@PathVariable int id){
        UserDao.deleteUser(id);
        return ResultVo.success("删除成功",true,null);
    }
}
